/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__WIDGET_H__
#define __XPINN__WIDGET_H__

#include <memory>
#include <list>
#include <map>
#include <functional>
#include <utility>
#include <XPWidgetDefs.h>
#include "xpinn.h"

class XPInnWidget
{
public:
  XPInnWidget(XPWidgetClass wclass, char const* desc = 0);
  XPInnWidget(std::pair<XPInnWidget*, std::pair<XPWidgetClass, char const*> > const&);
  ~XPInnWidget();

  XPInnWidget& addChild(XPWidgetClass wclass, char const* desc = 0);

  typedef std::list<XPInnWidget>::iterator iterator;
  typedef std::list<XPInnWidget>::const_iterator const_iterator;
  iterator begin() { return m_children.begin(); }
  const_iterator begin() const { return m_children.begin(); }
  iterator end() { return m_children.end(); }
  const_iterator end() const { return m_children.end(); }

  typedef std::tr1::function<void(XPInnWidget&)> visitor_func;
  typedef std::tr1::function<void(XPInnWidget const&)> const_visitor_func;
  void visitChildren(visitor_func const& v);
  void visitChildren(const_visitor_func const& v) const;

  typedef std::tr1::function<bool(xpsdk::intptr, xpsdk::intptr)> callback_func;
  void setCallback(XPWidgetMessage msg, callback_func const& func);

  class Layout
  {
  public:
    virtual ~Layout() {}
    virtual void performLayout(XPInnWidget&) = 0;
  };

  template <class T>
  void setLayout(T const& layout) { m_layout = std::auto_ptr<Layout>(new T(layout)); }

  Layout& layout() { return *m_layout; }
  Layout const& layout() const { return *m_layout; }

  enum size_enum
  {
    automatic = 0,
    expand,
  };

  struct SizeHint
  {
    enum
    {
      type_enum = 0,
      type_int,
      type_float,
    } m_type;
    union
    {
      size_enum m_enum;
      int m_int;
      float m_float;
    };
    SizeHint() : m_type(type_enum), m_enum(automatic) {}
    SizeHint(size_enum s) : m_type(type_enum), m_enum(s) {}
    SizeHint(int s) : m_type(type_int), m_int(s) {}
    SizeHint(float s) : m_type(type_float), m_float(s) {}
  };

  void setDesiredSize(SizeHint const& width, SizeHint const& height) { m_width = width; m_height = height; }

  SizeHint const& getDesiredWidth() const { return m_width; }
  SizeHint const& getDesiredHeight() const { return m_height; }

  enum halign_enum
  {
    align_left = 0,
    align_center,
    align_right,
  };
  enum valign_enum
  {
    align_top = 0,
    align_middle,
    align_bottom,
  };

  void setHAlign(halign_enum a) { m_halign = a; }
  void setVAlign(valign_enum a) { m_valign = a; }

  halign_enum getHAlign() const { return m_halign; }
  valign_enum getVAlign() const { return m_valign; }

  XPWidgetID getWidgetID() { return m_id; }

  void show();
  void hide();
  bool isVisible() const;
  void setGeometry(int left, int top, int right, int bottom);
  void setDescriptor(std::string const&);
  std::string getDescriptor() const;
  void setProperty(XPWidgetPropertyID prop, xpsdk::intptr value);
  xpsdk::intptr getProperty(XPWidgetPropertyID);

private:
  XPInnWidget(XPInnWidget& parent, XPWidgetClass wclass, char const* desc);

  XPInnWidget(XPInnWidget const&); // not implemented - forbid copying
  XPInnWidget& operator= (XPInnWidget const&); // not implemented - forbid copying

  static int callbackWrapper(XPWidgetMessage, XPWidgetID, xpsdk::intptr, xpsdk::intptr);
  static std::map<XPWidgetID, XPInnWidget*>& widgetsMap();

  bool applyLayout();
  void layoutVisitor();

  std::list<XPInnWidget> m_children;
  std::map<XPWidgetMessage, callback_func> m_callbacks;
  std::auto_ptr<Layout> m_layout;
  bool m_lockLayout;
  SizeHint m_width, m_height;
  halign_enum m_halign;
  valign_enum m_valign;
  XPWidgetID m_id;
};

class XPInnLayoutHorizontal : public XPInnWidget::Layout
{
public:
  virtual void performLayout(XPInnWidget&);
};

class XPInnLayoutVertical : public XPInnWidget::Layout
{
public:
  virtual void performLayout(XPInnWidget&);
};

class XPInnLayoutSuperimpose : public XPInnWidget::Layout
{
public:
  virtual void performLayout(XPInnWidget&);
};

#endif //__XPINN__WIDGET_H__